package com.syy.chart.controller;

import com.syy.chart.domain.WfForm;
import com.syy.chart.service.IWfFormService;
import com.syy.common.annotation.Log;
import com.syy.common.core.controller.BaseController;
import com.syy.common.core.domain.AjaxResult;
import com.syy.common.core.page.TableDataInfo;
import com.syy.common.enums.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 自定义表单
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/chquick_declarationrt/form_design")
public class FormController extends BaseController
{

    @Autowired
    private IWfFormService wfFormService;
    /**
     * 新增
     */
   /* @PreAuthorize("@ss.hasPermi('workflow:form:add')")*/
    @Log(title = "新增表单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody WfForm wfForm)
    {
        System.out.println(wfForm);
        return toAjax(wfFormService.addWfForm(wfForm));
    }



    /**
     * 查询代码生成列表
     */
    /*@PreAuthorize("@ss.hasPermi('workflow:form:list')")*/
    @GetMapping("/list")
    public TableDataInfo formList(WfForm wfForm)
    {
        startPage();
        List<WfForm> list = wfFormService.selectWfFormList(wfForm);
        return getDataTable(list);
    }


/*    @PreAuthorize("@ss.hasPermi('workflow:form:remove')")*/
    @Log(title = "删除表单", businessType = BusinessType.DELETE)
    @DeleteMapping("delete/{formIds}")
    public AjaxResult removeForm(@PathVariable String[] formIds)
    {
        int delCount = wfFormService.deleteWfFormByIds(formIds);
        if(delCount>0){
            return success();
        }else{
            return error();
        }
    }


/*    @PreAuthorize("@ss.hasPermi('workflow:form:edit')")*/
    @Log(title = "字段编辑", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult updateForm(@RequestBody WfForm wfForm)
    {

        int res = wfFormService.updateForm(wfForm);
        return toAjax(res);
    }


    @GetMapping("/getAllFields/{formId}")
    public AjaxResult getAllFields(@PathVariable("formId") String formId)
    {
        WfForm wfForm = wfFormService.getAllFields(formId);
        System.out.println(wfForm);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("wfForm", wfForm);
        return ajax;
    }
}
